package cn.cvs.servlet.supplier;

import cn.cvs.pojo.Supplier;
import cn.cvs.service.SupplierService;
import cn.cvs.util.Page;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "SuppierListServlet", urlPatterns = "/sys/supplier/list")
public class SuppierListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SupplierService supplierService = (SupplierService) SpringUtil.getBean ( "supplierService" );
        String pageNoStr = request.getParameter ( "pageNo" );
        String querySupCode = request.getParameter ( "querySupCode" );
        String querySupName = request.getParameter ( "querySupName" );
        request.setAttribute ( "querySupCode", querySupCode );
        request.setAttribute ( "querySupName", querySupName );

        Page<Supplier> page = new Page ( );
        page.setPageSize ( 5 );
        page.setTotalCount ( supplierService.getCountByCodeAndName ( querySupCode, querySupName ) );
        Integer pageNo=1;
        if (pageNoStr != null && pageNoStr != "") {
            pageNo=Integer.parseInt ( pageNoStr );
        }
        page.setPageNo ( pageNo );
        List<Supplier> list = supplierService.getSupplierListByCodeAndName ( querySupCode, querySupName, page.getPageNo ( ), page.getPageSize ( ) );
        page.setList (list );

        request.setAttribute ( "pageSupplier",page );
        System.out.println (page );
        request.getRequestDispatcher ( "/WEB-INF/jsp/supplier/list.jsp" ).forward ( request,response );
    }
}
